package com.example.bookmanagementsystem.entity;

import com.example.bookmanagementsystem.enums.ResultCode;
import lombok.Data;

@Data
public class Result<T> {
    //业务状态码
    private ResultCode resultCode;
    //错误信息
    private String errorMessage;
    //数据
    private T data;

    public Result(ResultCode resultCode, String errorMessage, T data) {
        this.resultCode = resultCode;
        this.errorMessage = (errorMessage != null) ? errorMessage : "";
        this.data = data;
    }

    // 成功返回结果
    public static <E> Result<E> success(E data) {
        return new Result<>(ResultCode.SUCCESS, "", data);
    }

    public static <E> Result<E> fail(String errorMessage) {
        Result<E> result = new Result<>(ResultCode.FAIL, errorMessage, null);
        return result;
    }

    public static <E> Result<E> fail(String errorMessage, E data) {
        Result<E> result = new Result<>(ResultCode.FAIL, errorMessage, data);
        return result;
    }
    public static <E> Result<E> unLogin() {
        Result<E> result = new Result<>(ResultCode.UN_LOGIN, "用户未登录", null);
        return result;
    }
}
